//
//  DarkFog.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/22/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "DarkFog.h"

@implementation DarkFog

-(id) init
{
    if (self = [super init])
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        float width = winSize.width;
        float height = winSize.height - uniPos(40, 80) * 2;
        mRenderTexture = [CCRenderTexture renderTextureWithWidth:width height:height];
        [mRenderTexture setPosition:ccp(winSize.width / 2, winSize.height / 2)];
        [self addChild:mRenderTexture];
        
        mSpotSprite = [[CCSprite spriteWithSpriteFrameName:SFN_SPOT_LIGHT] retain];
        [mSpotSprite setBlendFunc:(ccBlendFunc){GL_ZERO, GL_ONE_MINUS_SRC_ALPHA}];
        
    }
    
    return self;
}

-(void) draw
{
    [mRenderTexture beginWithClear:0.2 g:0.2 b:0.2 a:1.0];

    [mSpotSprite visit];
    
    [mRenderTexture end];  
    CC_INCREMENT_GL_DRAWS(1);
}

-(void) setSpotLightPosition:(CGPoint)position
{
    [mSpotSprite setPosition:ccp(position.x, position.y - OFFSET_Y_UI / 2)];
}

-(void) setSpotLightScale:(float)scale
{
    [mSpotSprite setScale:scale];
}

-(void) zoomIn
{
    [self setVisible:YES];
    [self schedule:@selector(updateZoomIn:)];
}

-(void) updateZoomIn:(ccTime)delta
{
    float scale = mSpotSprite.scale - delta * DARK_FOG_ZOOM_SPEED;
    if (scale <= DARK_FOG_ZOOM_IN)
    {
        scale = DARK_FOG_ZOOM_IN;
        [self unschedule:_cmd];
    }
    [mSpotSprite setScale:scale];
}

-(void) zoomOut
{
    [self schedule:@selector(updateZoomOut:)];
}

-(void) updateZoomOut:(ccTime)delta
{
    float scale = mSpotSprite.scale + delta * DARK_FOG_ZOOM_SPEED;
    if (scale >= DARK_FOG_ZOOM_OUT)
    {
        scale = DARK_FOG_ZOOM_OUT;
        [self unschedule:_cmd];
        [self setVisible:NO];
    }
    [mSpotSprite setScale:scale];
}

-(void) dealloc
{
    [super dealloc];
    CCLOG(@"Darkfog dealloc!");
}

@end
